{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47a1cc2f",
   "metadata": {},
   "source": [
    "------------------\n",
    "* 阶段：week10/11\n",
    "* 目标：实现web应用\n",
    "* 时间：2023-11-08"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9741930c",
   "metadata": {},
   "source": [
    "# 1.使用Flask搭建Web最小应用       \n",
    "> 1.使用select创建一个html页面：index.html          \n",
    "> 2.使用Flask中的render_template实现页面的路由跳转"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eb76c57",
   "metadata": {},
   "source": [
    "# 本周内容：Web应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a7b8b62f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [22/Nov/2023 17:33:55] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [22/Nov/2023 17:33:55] \"GET /favicon.ico HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [22/Nov/2023 17:33:57] \"POST /user_choose HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [22/Nov/2023 17:34:02] \"POST /user_choose HTTP/1.1\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return render_template('index.html')\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb43cb11",
   "metadata": {},
   "source": [
    "## 2.提交表单的内容在Python中接收"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5348850d",
   "metadata": {},
   "source": [
    "## 1.html展示抓取数据table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45c7402d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [22/Nov/2023 17:34:24] \"GET / HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全国\n",
      "产品经理\n",
      "应届生\n",
      "登录cookie XSRF-TOKEN=zIARnRM0QQaLudJk_4OBXw; __gc_id=e936feed5bd343e5a73b304fa0d4ad5f; _ga=GA1.1.504776059.1697021947; __uuid=1697021948310.51; __tlog=1697021948354.48%7C00000000%7C00000000%7C00000000%7C00000000; Hm_lvt_a2647413544f5a04f00da7eee0d5e200=1697021951; acw_tc=2760828416970219810274366e53a098388abcfc886c89f5cbf2332344893f; Hm_lpvt_a2647413544f5a04f00da7eee0d5e200=1697021975; __session_seq=4; __uv_seq=4; __tlg_event_seq=24; _ga_54YTJKWN86=GS1.1.1697021946.1.1.1697023027.0.0.0\n",
      "这是第1页，接下来将先等待8秒...然后以继续抓取\n",
      "这是第2页，接下来将先等待7秒...然后以继续抓取\n",
      "这是第3页，接下来将先等待10秒...然后以继续抓取\n",
      "这是第4页，接下来将先等待8秒...然后以继续抓取\n",
      "这是第5页，接下来将先等待8秒...然后以继续抓取\n",
      "这是第6页，接下来将先等待8秒...然后以继续抓取\n",
      "这是第7页，接下来将先等待3秒...然后以继续抓取\n",
      "这是第8页，接下来将先等待10秒...然后以继续抓取\n",
      "这是第9页，接下来将先等待3秒...然后以继续抓取\n",
      "这是第10页，接下来将先等待6秒...然后以继续抓取\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [22/Nov/2023 17:35:44] \"POST /user_choose HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template,request\n",
    "import crawl_liepin\n",
    "from jinja2 import Markup, Environment, FileSystemLoader\n",
    "from pyecharts.globals import CurrentConfig\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Map\n",
    "\n",
    "# 关于 CurrentConfig，可参考 [基本使用-全局变量]\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "@app.route('/user_choose',methods = ['POST', 'GET'])\n",
    "def user_choose():\n",
    "    print(request.form['city'])\n",
    "    print(request.form['category'])\n",
    "    print(request.form['workexp'])\n",
    "    user_city = request.form['city']\n",
    "    user_category = request.form['category']\n",
    "    user_workexp = request.form['workexp']\n",
    "    crawl_results = crawl_liepin.crawl(城市 = user_city ,关键词=user_category,工作经验=user_workexp)\n",
    "    crawl_results_df = crawl_results[\n",
    "        [\n",
    "            'recruiter.recruiterTitle',\n",
    "            'comp.compScale',\n",
    "            'comp.compStage',\n",
    "            'comp.compIndustry',\n",
    "            'comp.compName',\n",
    "            'job.labels',\n",
    "            'job.dq',\n",
    "            'job.campusJobKind',\n",
    "            'job.title',\n",
    "            'job.salary'\n",
    "        ]\n",
    "    ]\n",
    "    crawl_results_df.to_excel('%s_%s_%s.xlsx'%(user_city,user_category,user_workexp))\n",
    "    crawl_results_html = crawl_results_df.to_html()\n",
    "    # 文件存储\n",
    "    text_file = open(\"templates/results.html\", \"w\",encoding=\"utf-8\")\n",
    "    text_file.write(crawl_results_html)\n",
    "    text_file.close()\n",
    "    return render_template(\"results.html\")\n",
    "\n",
    "def map_base() -> Map:\n",
    "    df = pd.read_excel('北京_产品运营_应届生.xlsx')\n",
    "    series_dq = df['job.dq'].value_counts() # 整理地区的区域数据\n",
    "    dq_name= [i for i in series_dq.index if '-' in i] # 梳理区域名称\n",
    "    series_dq[dq_name].values #　梳理各区域的数据\n",
    "    c = (\n",
    "        Map()\n",
    "        .add(\"产品经理地区分布\", [list(z) for z in zip([i.split('-')[1] for i in dq_name],series_dq[dq_name].values.tolist())], \"北京\")\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=\"Map-北京_产品运营_应届生\"), visualmap_opts=opts.VisualMapOpts()\n",
    "        )\n",
    "\n",
    "    )\n",
    "    \n",
    "    return c\n",
    "\n",
    "\n",
    "@app.route(\"/map\")\n",
    "def map_test():\n",
    "    c = map_base()\n",
    "    return Markup(c.render_embed())                        \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73402e38",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "471ba343",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
